//
// Created by Administrator on 2021/11/26.
//

#include "Splatting.h"

void Splatting::foot_print(int diameter) {
    float m;
    int radius = (diameter - 1) / 2;
    float x, y, z;

    for (int i = 0; i < diameter; i++) {
        for (int j = 0; j < diameter; j++) {
            for (int k = 0; k < diameter; k++) {
                x = (float) (i - radius) / radius;
                y = (float) (j - radius) / radius;
                z = (float) (k - radius) / radius;

                if (x * x + y * y + z * z <= 1) {
                    m = 1 - std::sqrt(x * x + y * y + z * z);
                    table[i][j] += (float) m * 1 / radius;
                }
            }
        }
    }
}

Splatting::Splatting(RawData *rawData) : rawData(rawData) {
    foot_print(DIAMETER);
}

void Splatting::reset_value() {
    for (auto &x: value) {
        for (float &y: x) {
            y = 0;
        }
    }
}

void Splatting::exec(int i) {
    float density, density_table;
    auto data = rawData->get_data();

    for (int j = 2; j < VOLUME_Z - 2; j++) {
        for (int k = 2; k < VOLUME_Y - 2; k++) {
            density = (float) data[j * 256 * 256 + k * 256 + i];

            for (int x = 0; x < DIAMETER; x++)
                for (int y = 0; y < DIAMETER; y++) {
                    density_table = density * table[x][y];
                    value[j * 2 - (DIAMETER - 1) / 2 + y][k * 2 - (DIAMETER - 1) / 2 + x] += density_table;
                }
        }
    }
}
